package com.util;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* renamed from: com.hhzt.iptv.module.common.utils.StringUtils */
public class StringUtils {
    private static final Pattern emailer = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    private static final Pattern phone = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");


    public static String substring(String src, String begin, String end) {
        //http://192.168.1.250:8080/upload/backgroundimg/1556787880526.png?md5=02297BE6C0097A66F96898F3484456E5"
        int b = src.lastIndexOf(begin);
        int e = src.indexOf(end, b);
        if (b > -1 && e > -1)
            return src.substring(b + begin.length(), e);

        return "";

    }

    public static boolean isEmpty(CharSequence input) {
        if (input == null || "".equals(input)) {
            return true;
        }
        return false;
    }

    public static boolean isNull(CharSequence input) {
        if (input == null || "".equals(input)) {
            return true;
        }
        return false;
    }

    public static boolean isEmail(CharSequence email) {
        if (StringUtils.isEmpty(email)) {
            return false;
        }
        return emailer.matcher(email).matches();
    }

    public static boolean isPhone(CharSequence phoneNum) {
        if (StringUtils.isEmpty(phoneNum)) {
            return false;
        }
        return phone.matcher(phoneNum).matches();
    }

    public static int toInt(String str, int defValue) {
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            e.printStackTrace();
            return defValue;
        }
    }

    public static int toInt(Object obj) {
        if (obj == null) {
            return 0;
        }
        return StringUtils.toInt(obj.toString(), 0);
    }

    public static long toLong(String obj) {
        try {
            return Long.parseLong(obj);
        } catch (Exception e) {
            return 0;
        }
    }

    public static double toDouble(String obj) {
        try {
            return Double.parseDouble(obj);
        } catch (Exception e) {
            return 0.0d;
        }
    }

    public static boolean toBool(String b) {
        try {
            return Boolean.parseBoolean(b);
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean isNumber(CharSequence str) {
        try {
            Integer.parseInt(str.toString());
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static final String byteArrayToHexString(byte[] data) {
        StringBuilder sb = new StringBuilder(data.length * 2);
        for (byte b : data) {
            int v = b & 255;
            if (v < 16) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(v));
        }
        return sb.toString().toUpperCase(Locale.getDefault());
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] d = new byte[(len / 2)];
        for (int i = 0; i < len; i += 2) {
            d[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
        }
        return d;
    }

    public static boolean isEmpty(String str) {
        if (str == null || str.length() == 0)
            return true;
        return false;
    }

    public static String getLastfix(String url) {
        int index = url.lastIndexOf('.');
        if (index > -1) {
            return url.substring(index);
        }
        return null;
    }

    public static String generateTime(double time) {
        return null;
    }

    /**
     * 获取URL后面参数
     *
     * @param name
     * @return
     */
    public static String getURLParam(String url, String name) {
        url += "&";
        String pattern = "(\\?|&){1}#{0,1}" + name + "=[a-zA-Z0-9]*(&{1})";

        Pattern r = Pattern.compile(pattern);

        Matcher m = r.matcher(url);
        if (m.find()) {
            // System.out.println(m.group(0));
            return m.group(0).split("=")[1].replace("&", "");
        } else {
            return null;
        }

    }

    /**
     * 获取文件夹
     *
     * @param url
     * @return
     */
    public static String getDir(String url) {
        if (url==null) return "";
        int index = url.lastIndexOf('/');
        String dir = url;
        if (index>-1) {
           dir = url.substring(0, index);
        }
        if (dir.startsWith(".")) {
            dir = dir.substring(1);
        }
        if (dir.startsWith("http://")){
            int index2 = dir.indexOf('/',8);
            dir = dir.substring(index2+1);
        }
        return dir;
    }

    public static String getFilename(String url) {
        if (url==null)
            return "";
        int index = url.lastIndexOf('/');
        int index2 = url.lastIndexOf('?');
        if (index2 == -1)
            return url.substring(index + 1);


        return url.substring(index + 1, index2);
    }

    public static void main(String[] args) {
        String url = "./screen/upload/2020-01-28/7348ad0781b0485a94893f3b60881eda.png?md5=D55D8EECCBD9D456684B3AFC59584DB5";
        String md5 = getURLParam(url, "md5");

        String dir = getDir(url);

        String name = getFilename(url);

        System.out.println(md5);
        System.out.println(dir);
        System.out.println(name);

    }
}
